// packageI/pages/storageRecord/storageRecord.js
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    token: wx.getStorageSync('TOKEN'),
    storeList: [],
    current: '',
    show: false,
    areaList: [{
      "name": "请选择区域"
    }],
    currentIndex: 0,
    id: 0
  },

  changeList(e) {
    var that = this
    that.data.current = e.currentTarget.dataset.type
    that.setData(that.data)
    that.getInfos()
  },

  changeCurrentIndex(e) {
    var that = this
    that.setData({
      currentIndex: e.detail.value
    })
  },

  getArea() {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/warehouse/list',
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          that.data.areaList = that.data.areaList.concat(res.data.data)
          that.setData(that.data)
        }
      }
    })
  },

  getInfos() {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/consumableBillDetail/permissionBillDetails',
      method: 'GET',
      data: {
        billType: that.data.current
      },
      header: {
        Authorization: that.data.token
      },
      success: function (res) {
        if (res.data.code == 0) {
          for (var i in res.data.data) {
            if (res.data.data[i].images != null) {
              res.data.data[i].images = res.data.data[i].images.split(',')[0]
            }
          }
          that.data.storeList = res.data.data
          that.setData(that.data)
        }
      }

    })
  },
  ruku(e) {
    var that = this
    that.data.show = !that.data.show
    that.data.id = e.currentTarget.dataset.id
    that.setData(that.data)
  },
  submit() {
    var that = this
    if (that.data.currentIndex == 0) {
      wx.showToast({
        title: '请选择区域',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: applet_park_api_url + '/api/consumableBillDetail/inWarehouse',
      method: 'post',
      header: {
        Authorization: that.data.token
      },
      data: {
        id: that.data.id,
        warehouseId: that.data.areaList[that.data.currentIndex].id
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.data.show = false
          that.getInfos()
          wx.showToast({
            title: '操作成功',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },

  quyong(e) {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/consumableBillDetail/outWarehouse/' + e.currentTarget.dataset.id,
      method: 'post',
      header: {
        Authorization: that.data.token
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.data.show = false
          that.getInfos()
          wx.showToast({
            title: '操作成功',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getInfos()
    that.getArea()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})